package com.epam.debrecen.hackathon.ai.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.PriorityQueue;

public class BattleState implements Comparable<State>, State {

	private BattleState parent;

	private final Hand hand;

	private Cards cards;

	private Players players;

	private String selectedCard;

	public BattleState(Hand hand) {
		this.hand = hand;
	}

	public BattleState(BattleState state, String selectedCard) {
		this.selectedCard = selectedCard;
		this.hand = new Hand(this.createCopy(state, selectedCard));
		this.cards = state.cards;
		this.players = state.players;
	}

	private List<String> createCopy(BattleState state, String selectedCard) {
		List<String> newList = new ArrayList<String>();
		for (String card : state.getHand().getCards()) {
			newList.add(card);
		}

		newList.remove(selectedCard);

		return newList;
	}

	public Hand getHand() {
		return this.hand;
	}

	public BattleState getParent() {
		return this.parent;
	}

	public String getSelectedCard() {
		return this.selectedCard;
	}

	@Override
	public int compareTo(State o) {
		BattleState bs = (BattleState) o;
		return bs.getValue() - this.getValue();
	}

	@Override
	public Collection<State> getPossibleStates() {
		PriorityQueue<State> nextStates = new PriorityQueue<State>();
		for (String card : this.hand.getCards()) {
			nextStates.add(new BattleState(this, card));
		}
		return nextStates;
	}

	@Override
	public Collection<State> getChildren() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public State getAncestor() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public int getDepth() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setCards(Cards cards) {
		this.cards = cards;
	}

	public void setPlayers(Players players) {
		this.players = players;
	}

	@Override
	public int getValue() {

		Card c = Card.fromName(this.selectedCard);

		/*if (c == Card.WINTER) {
			for (String cName : this.hand.getCards()) {
				Card c2 = Card.fromName(cName);
				if (c2 == Card.HEROINE) {
					return c.getValue() + 7;
				}
			}
		}


		if (this.cards.isWinter()) {
			if (c == Card.HEROINE) {
				return Card.HEROINE.getValue() * 2;
			} else {
				return -c.getValue();
			}
		}

		if (this.cards.isSpring()) {
			if (c.isMercenary()) {
				return c.getValue() + 3;
			} else if (c == Card.HEROINE) {
				return c.getValue() - 3;
			}
		}
		int myLineCount = cards.getCurrentPlayerLineValue(players);
		int enemyLineCount = cards.countBiggestLineValue(players);
		if(c == Card.DRUMMER && myLineCount > enemyLineCount/2 && myLineCount < enemyLineCount){
				return c.getValue()+6;
		}
		if(c == Card.COURTESAN && myLineCount < enemyLineCount - 4){
			return c.getValue() + 1;
		}
		/*if(c == Card.SCARECROW && cards.isWinter() && cards.hasHighValueMerc(players)){
			return c.getValue() +5;
		}
		if(c == Card.BISHOP && enemyLineCount > myLineCount+10){
			return c.getValue()+5;
		}*/
		return Card.fromName(this.selectedCard).getValue();
	}

}
